home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** File: Preferences.c
- ** Written by: Eric Soldan
- **
- ** Copyright © 1989-1993 Apple Computer, Inc.
- ** All rights reserved.
- */
-
- /* You may incorporate this sample code into your applications without
- ** restriction, though the sample code has been provided "AS IS" and the
- ** responsibility for its operation is 100% yours. However, what you are
- ** not permitted to do is to redistribute the source as "DSC Sample Code"
- ** after having made changes. If you're going to re-distribute the source,
- ** we require that you make it clear in the source that the code was
- ** descended from Apple Sample Code, but that you've made changes. */
-
-
-
-
- /*****************************************************************************/
-
-
-
- #include "DTS.Lib2.h"
- #include "DTS.Lib.protos.h"
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __FOLDERS__
- #include <Folders.h>
- #endif
-
- #ifndef __GESTALTEQU__
- #include <GestaltEqu.h>
- #endif
-
- #ifndef __RESOURCES__
- #include "Resources.h"
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
-
-
- /*****************************************************************************/
-
-
-
- extern short gAUXVersion;
- extern GetPreferenceProcPtr gGetPreferenceProc;
-
- TreeObjHndl gPreferences;
- static FSSpec gPrefsLoc = {0, 0, "\p"};
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
- #ifdef applec
- #pragma segment ATGPreferences
- #endif
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- OSErr ReadPreferences(OSType prefType, OSType prefCreator, short prefLocID)
- {
- OSErr err;
- short res, oldRes;
- long dirID;
- Str31 prefsFile;
- Handle hndl;
- FInfo finfo;
-
- gGetPreferenceProc = GetPreference;
- /* Prevent dead-stripping if ReadPreferences is called. If thisis called,
- ** the application is committing to preferences. */
-
- GetIndString(gPrefsLoc.name, prefLocID, 1);
- GetIndString(prefsFile, prefLocID, 2);
- /* Get the application's preference folder/file names to create/open. */
-
- if (gSystemVersion >= 0x0700)
- err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
- &gPrefsLoc.vRefNum, &gPrefsLoc.parID);
- else
- err = FindSysFolder(&gPrefsLoc.vRefNum, &gPrefsLoc.parID);
-
- if (!err) {
- err = DirCreate(gPrefsLoc.vRefNum, gPrefsLoc.parID, gPrefsLoc.name, &dirID);
- if (err == dupFNErr) err = noErr;
- }
-
- if (!err) {
- oldRes = CurResFile();
-
- BlockMove(gPrefsLoc.name + 1, gPrefsLoc.name + 2, gPrefsLoc.name[0]++);
- gPrefsLoc.name[1] = (gAUXVersion) ? '/' : ':';
- pcatchr(gPrefsLoc.name, gPrefsLoc.name[1], 1);
- pcat (gPrefsLoc.name, prefsFile);
-
- HCreateResFile(gPrefsLoc.vRefNum, gPrefsLoc.parID, gPrefsLoc.name);
- err = ResError();
- if (!err) {
- err = HGetFInfo(gPrefsLoc.vRefNum, gPrefsLoc.parID, gPrefsLoc.name, &finfo);
- if (!err) {
- finfo.fdType = prefType;
- finfo.fdCreator = prefCreator;
- err = HSetFInfo(gPrefsLoc.vRefNum, gPrefsLoc.parID, gPrefsLoc.name, &finfo);
- }
- }
- if (err == dupFNErr) err = noErr;
- if (err) {
- UseResFile(oldRes);
- return(err);
- }
-
- SetResLoad(false);
- res = HOpenResFile(gPrefsLoc.vRefNum, gPrefsLoc.parID, gPrefsLoc.name, fsRdPerm);
- err = ResError();
- SetResLoad(true);
- if (err) {
- UseResFile(oldRes);
- return(err);
- }
-
- gPreferences = NewRootObj(ROOTOBJ, 0);
- if (!gPreferences) {
- CloseResFile(res);
- UseResFile(oldRes);
- return(memFullErr);
- }
-
- UseResFile(res);
- hndl = GetResource('aprf', 128);
- if (!hndl) {
- CloseResFile(res);
- UseResFile(oldRes);
- return(resNotFound);
- } /* Read in file's preference resource. If it doesn't exist in the pref's file,
- ** then it is gotten out of the app. If it doesn't exist there,then return an
- ** error. The app should resolve this by creating the default preference info. */
-
- DetachResource(hndl);
- CloseResFile(res);
- UseResFile(oldRes);
-
- err = HReadTree(gPreferences, hndl);
- /* Note that you CAN save preference data in the root object, since the min size
- ** of the root object is determined by the app (in File.c). However, the
- ** convention for the preference hierarchy is to have a dumb root object, and
- ** then hang objects off of the root of type APRFOBJ. The APRFObj contains the
- ** window state data, and this data will be managed automatically by the framework
- ** (if a document of the OSType indicated by the APRFObj is opened, for example).
- ** The APRFObj data area is therefore off limits to the application. HOWEVER,
- ** there is no restriction to adding children to the APRFObj. The children of
- ** the APRFObj can be of any type you want. Therefore application-specific
- ** preference data should be saved below the APRFObj level. */
-
- DisposeHandle(hndl);
- }
-
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- OSErr WritePreferences(void)
- {
- short oldRes, res;
- Handle hndl;
- OSErr err;
-
- oldRes = CurResFile();
- SetResLoad(false);
- res = HOpenResFile(gPrefsLoc.vRefNum, gPrefsLoc.parID, gPrefsLoc.name, fsRdWrPerm);
- err = ResError();
- SetResLoad(true);
- if (err) {
- UseResFile(oldRes);
- return(err);
- }
-
- UseResFile(res);
- hndl = GetResource('aprf', 128);
- if (hndl) {
- RemoveResource(hndl);
- DisposeHandle(hndl);
- }
-
- if (gPreferences) {
- hndl = NewHandle(0);
- if (hndl) {
- err = HWriteTree(gPreferences, hndl);
- if (!err) {
- AddResource(hndl, 'aprf', 128, nil);
- ChangedResource(hndl);
- WriteResource(hndl);
- DetachResource(hndl);
- }
- DisposeHandle(hndl);
- }
- }
-
- CloseResFile(res);
- UseResFile(oldRes);
-
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- TreeObjHndl GetPreference(OSType prefType, Boolean *newPref)
- {
- short i;
- TreeObjHndl cobj;
-
- if (newPref) *newPref = false;
-
- if (!gPreferences) return(nil);
- /* Shouldn't ever happen if ReadPreferences was called. (See ReadPreferences.) */
-
- for (i = (*gPreferences)->numChildren; i;) {
- cobj = GetChildHndl(gPreferences, --i);
- if (mDerefAPRF(cobj)->sfType == prefType) return(cobj);
- }
-
- if (!newPref) return(nil);
- /* We only automatically create one if we are passed a pointer to indicate if
- ** the preference returned is new. This way newPref can also serve as a flag. */
-
- cobj = NewChild(NO_EDIT, gPreferences, -1, APRFOBJ, sizeof(APRFObj));
- if (cobj) {
- mDerefAPRF(cobj)->sfType = prefType;
- *newPref = true;
- }
-
- return(cobj);
- }
-
-
-
-